//
//  YHCirculationView.m
//  图片动画
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "YHCirculationView.h"
#import "UIImageView+WebCache.h"
#import "AllHeader.h"
#import "FitHeader.h"

@interface YHCirculationView ()
@property(nonatomic, strong)UIImageView *imageView;
@property(nonatomic, assign)NSInteger currentIndex;//图片当前坐标
@property(nonatomic, strong)CATransition *transition;
@property(nonatomic, strong)NSMutableArray *imageDictArray;//图片字典数组
@property(nonatomic, strong)UIPageControl *pageControl;//小白点
@property(nonatomic, strong)UILabel *titleLabel;
//@property(nonatomic, strong)UIImageView *image_urlImageView;
@end

@implementation YHCirculationView

#pragma mark - 自定义初始化
- (instancetype)initWithFrame:(CGRect)frame ImageDictArray:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        //初始化图片字典数组
        _imageDictArray = [NSMutableArray arrayWithArray:array];
        //删除广告
        for (NSDictionary *dict in array) {
            if ([[[dict objectForKey:@"tag_info"] objectForKey:@"text"] isEqualToString:@"广告"]) {
                [_imageDictArray removeObject:dict];
            }
        }
        //创建视图
        [self createViews];
    }
    return self;
}

#pragma mark - 创建视图
- (void)createViews
{
    //创建imageView
    _imageView = [[UIImageView alloc] init];
    [_imageView sd_setImageWithURL:[NSURL URLWithString:[_imageDictArray[0] objectForKey:@"promotion_img"]] placeholderImage:[UIImage imageNamed:@"PlaceHolder"]];
    _imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [_imageView addGestureRecognizer:tap];
    [self addSubview:_imageView];
    
    //标题
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.text = [_imageDictArray[0] objectForKey:@"title"];
    _titleLabel.font = [UIFont fontWithBold:1 IPhone6sPlus:16 Iphone6s:15 Iphone5s:14 Iphone4s:13];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.shadowColor = [UIColor blackColor];
    _titleLabel.highlighted = YES;
    _titleLabel.shadowOffset = CGSizeMake(-.1, .7);
    [_imageView.layer addSublayer:_titleLabel.layer];
    
    //图片类型
//    _image_urlImageView = [[UIImageView alloc]init];
//    [_image_urlImageView sd_setImageWithURL:[NSURL URLWithString:[[_imageDictArray[0] objectForKey:@"tag_info"] objectForKey:@"image_url"]] placeholderImage:nil];
//    [_imageView.layer addSublayer:_image_urlImageView.layer];

    //向左轻扫手势
    UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe:)];
    leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [_imageView addGestureRecognizer:leftSwipe];
    
    //向右轻扫手势
    UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe:)];
    rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
    [_imageView addGestureRecognizer:rightSwipe];
    
    //创建小白点
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 20, self.frame.size.width, 20)];
    _pageControl.numberOfPages = _imageDictArray.count;
    [self addSubview:_pageControl];
}

#pragma mark 布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    _imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
    _titleLabel.frame = CGRectMake(20 * FITWIDTH, 170 * FITHEIGHT, 375 * FITWIDTH, 26 * FITHEIGHT);
    
//    _image_urlImageView.frame = CGRectMake(352 * FITWIDTH, 13 * FITHEIGHT, 23 * FITWIDTH, 13 * FITHEIGHT);
}

#pragma mark - 手势方法
#pragma mark 向左轻扫方法
- (void)leftSwipe:(UISwipeGestureRecognizer *)swipe
{
    [self transitionAnimation:YES];
}
#pragma mark 向右轻扫方法
- (void)rightSwipe:(UISwipeGestureRecognizer *)swipe
{
    [self transitionAnimation:NO];
}
#pragma mark 点击方法
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    if ([_imageDictArray[_currentIndex] objectForKey:@"article"]) {
        WFChannelDetailModel *model = [WFChannelDetailModel new];
        [model setValuesForKeysWithDictionary:[_imageDictArray[_currentIndex] objectForKey:@"article"]];
        [self.circulationDelegate passWebViewModel:model];
    }
    else if ([[[_imageDictArray[_currentIndex] objectForKey:@"tag_info"] objectForKey:@"text"] isEqualToString:@"频道"]) {
        [InternetDataRequest getDataByString:[[_imageDictArray[_currentIndex] objectForKey:@"block_info"] objectForKey:@"api_url"] BodyDic:nil WithDataBlock:^(id data) {
            NSArray *articlesArray = [[(NSDictionary *)data objectForKey:@"data"] objectForKey:@"articles"];
            WFChannelDetailModel *model = [WFChannelDetailModel new];
            for (NSDictionary *dict in articlesArray) {
                if ([[dict objectForKey:@"title"] isEqualToString:_titleLabel.text]) {
                    [model setValuesForKeysWithDictionary:dict];
                    [self.circulationDelegate passWebViewModel:model];
                    break;
                }
            }
        }];
    }
    else {
//        NSLog(@"不能点");
    }
}

#pragma mark - 切换图片动画
- (void)transitionAnimation:(BOOL)isLeft
{
    //创建动画
    _transition = [[CATransition alloc] init];
    //设置动画类型
    _transition.type = [self getTransitionTypeString];
    
    //向左轻扫
    if (isLeft) {
        _transition.subtype = kCATransitionFromRight;
    }
    //向右轻扫
    else {
        _transition.subtype = kCATransitionFromLeft;
    }
    _transition.duration = .5;
    
    [_imageView sd_setImageWithURL:[self getImageURLString:isLeft] placeholderImage:nil];
//    [_image_urlImageView sd_setImageWithURL:[NSURL URLWithString:[[_imageDictArray[_currentIndex] objectForKey:@"tag_info"] objectForKey:@"image_url"]] placeholderImage:[UIImage imageNamed:@""]];
    [_imageView.layer addAnimation:_transition forKey:@"KCTransitionAnimation"];
}
#pragma mark 获取动画类型
- (NSString *)getTransitionTypeString
{
    switch (_yhTransitionStyle) {
        case 0:
            return @"cube";
        case 1:
            return @"oglFlip";
        case 2:
            return @"suckEffect";
        case 3:
            return @"rippleEffect";
        case 4:
            return @"pageCurl";
        case 5:
            return @"pageUnCurl";
        case 6:
            return @"cameralIrisHollowOpen";
        case 7:
            return @"cameraIrisHollowClose";
        case 8:
            return @"fade";
        case 9:
            return @"movein";
        case 10:
            return @"push";
        case 11:
            return @"reveal";
            
        default:
            return @"cube";
    }
}
#pragma mark 获取图片url
- (NSURL *)getImageURLString:(BOOL)isLeft
{
    //向左轻扫
    if (isLeft) {
        _currentIndex = (_currentIndex + 1) % _imageDictArray.count;
    }
    //向右轻扫
    else {
        _currentIndex = (_currentIndex - 1 + _imageDictArray.count) % _imageDictArray.count;
    }
    
    //改变title及小白点坐标
    [self changePageControlIndex];
    
    
    return [NSURL URLWithString:[_imageDictArray[_currentIndex] objectForKey:@"promotion_img"]];
}

#pragma mark - 重写属性set
- (void)setYhTransitionStyle:(YHTransitionStyle)yhTransitionStyle
{
    _yhTransitionStyle = yhTransitionStyle;
    _transition.type = [self getTransitionTypeString];
    
}

#pragma mark - 改变title及小白点坐标
- (void)changePageControlIndex
{
    _pageControl.currentPage = _currentIndex;
    _titleLabel.text = [_imageDictArray[_currentIndex] objectForKey:@"title"];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
